package com.dq.service.config.impl;

import com.dq.exception.customException.AdminException;
import com.dq.domain.evaluation.EvaluationTemplateChild;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.config.EvaluationTemplateChildService;
import com.dq.service.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 *
 * 岗位逻辑
 */
@Service
public class EvaluationTemplateChildServiceImpl extends BaseRepositories implements EvaluationTemplateChildService {
    @Autowired
    private SysUserService sysUserService;



    /**
     * 添加/修改, 批量
     * @param list
     * @return
     * @throws AdminException
     */
    @Override
    public List<EvaluationTemplateChild> saveUpdateList(List<EvaluationTemplateChild> list)  throws AdminException {
        if(list == null || list.size() <= 0) {
            throw new AdminException("集合为空");
        }
        List<EvaluationTemplateChild> listSave = new ArrayList<>();
        for (EvaluationTemplateChild evaluationTemplateChild : list) {
            if(evaluationTemplateChild == null) {
                throw new AdminException("保存对象为空");
            }
            if (evaluationTemplateChild.getId() != null) {
                evaluationTemplateChild = objectUpdateBase(evaluationTemplateChild);
            } else {
                evaluationTemplateChild = objectSaveBase(evaluationTemplateChild);
            }

            listSave.add(evaluationTemplateChild);
        }

        dao.saveOrModifyList(listSave);
        return listSave;
    }

    /**
     * 添加/修改, 模板子项
     */
    @Override
    public EvaluationTemplateChild saveUpdate(@Valid EvaluationTemplateChild evaluationTemplateChild)  throws AdminException {
        if(evaluationTemplateChild == null) {
            throw new AdminException("保存对象为空");
        }
        if (evaluationTemplateChild.getId() != null) {
            evaluationTemplateChild = objectUpdateBase(evaluationTemplateChild);
        } else {
            evaluationTemplateChild = objectSaveBase(evaluationTemplateChild);
        }

        return evaluationTemplateChildRepository.save(evaluationTemplateChild);
    }

    /**
     * 添加对象基本属性配置
     */
    @Override
    public EvaluationTemplateChild objectSaveBase(EvaluationTemplateChild evaluationTemplateChild) throws AdminException {
        SysUser user = sysUserService.getUser();
        evaluationTemplateChild.setCreateUser(user);
        evaluationTemplateChild.setUpdateUser(user);

        return evaluationTemplateChild;
    }
    /**
     * 修改对象基本属性配置
     */
    @Override
    public EvaluationTemplateChild objectUpdateBase(EvaluationTemplateChild evaluationTemplateChild) throws AdminException {
        SysUser user = sysUserService.getUser();
        if(user != null) {
            evaluationTemplateChild.setUpdateUser(user);
        }

        return evaluationTemplateChild;
    }

}

